﻿using Locker.FrameWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Locker
{
    /// <summary>
    /// Interaction logic for WelcomeForm.xaml
    /// </summary>
    public partial class WelcomeForm : Window
    {
        private PasswordProtectionForm passwordProtectedForm;
        private MainWindow loginForm;
        private LoginDetails loginCred;
        public string SetErrorMessage
        {
            set
            {
                lblErrorMsg.Visibility = System.Windows.Visibility.Visible;
                lblErrorMsg.Content = value;
            }
        }
        public WelcomeForm()
        {
            try
            {
                loginCred = ActivatedUsers.CheckUserAlreadyExists();
                if (loginCred != null)
                {
                    loginForm = new MainWindow();
                    this.Close();                    
                    loginForm.Show();
                }
            }
            catch { }
            InitializeComponent();
        }

        private void CreateAccount_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(txtUserName.Text) && txtUserName.Text.Length >= 4)
                {
                    if (!string.IsNullOrEmpty(txtPassword.Password) && txtPassword.Password.Length >= 6)
                    {
                        if (txtPassword.Password == txtConfirmPassword.Password)
                        {
                            // Code to insert into database and redirect to history page
                            try
                            {
                                LoginDetails loginCred = new LoginDetails();
                                loginCred.UserNameOrEmail = txtUserName.Text;
                                loginCred.PasswordHash = txtPassword.Password;
                                Task<bool> taskAddNewuser = Task.Factory.StartNew(() => ActivatedUsers.AddNewUserToLocker(loginCred));
                                taskAddNewuser.ContinueWith((isSuccess) =>
                                {
                                    if (isSuccess.Result)
                                    {
                                        passwordProtectedForm = new PasswordProtectionForm(loginCred);
                                        this.Close();
                                        passwordProtectedForm.Show();
                                    }
                                    else
                                        SetErrorMessage = "Something went wrong.. Try again..";
                                }, TaskScheduler.FromCurrentSynchronizationContext());
                            }
                            catch
                            {
                                SetErrorMessage = "Something went wrong.. Try again..";
                            }
                        }
                        else
                            SetErrorMessage = "Password do not match";
                    }
                    else
                        SetErrorMessage = "Please Enter valid Password";
                }
                else
                    SetErrorMessage = "Please enter valid User Name or Email";
            }
            catch (Exception ex)
            {
                SetErrorMessage = "Something went wrong.. Try again..";
            }
        }

        private void ResetFormControlFields()
        {
            txtPassword.Password = "";
            txtConfirmPassword.Password = "";
            lblErrorMsg.Content = "";
            lblErrorMsg.Visibility = System.Windows.Visibility.Hidden;
        }
    }
}
